Bashcomp: improve performance
authorgentoo90 <gentoo90@gmail.com>
Sun, 26 Jul 2015 11:21:44 +0000 (14:21 +0300)
committergentoo90 <gentoo90@gmail.com>
Sun, 26 Jul 2015 11:21:44 +0000 (14:21 +0300)
Calling `cargo --list` on every completion is too slow.
Call it once on bash start and cache the output.

src/etc/cargo.bashcomp.sh

index d3a1649a8728bdc701a8189e591acd6a5b37ecfe..96860136599d7064eaf7788a388f6fc6a0639131 100644 (file)
@@ -8,7 +8,6 @@ _cargo()
 
        cmd=${words[1]}
 
-       local commands=$(cargo --list | tail -n +2)
        local vcs='git hg none'
 
        local opt_help='-h --help'
@@ -49,7 +48,7 @@ _cargo()
                if [[ "$cur" == -* ]]; then
                        COMPREPLY=( $( compgen -W "${opt___nocmd}" -- "$cur" ) )
                else
-                       COMPREPLY=( $( compgen -W "$commands" -- "$cur" ) )
+                       COMPREPLY=( $( compgen -W "$__cargo_commands" -- "$cur" ) )
                fi
        elif [[ $cword -ge 2 ]]; then
                case "${prev}" in
@@ -63,7 +62,7 @@ _cargo()
                                COMPREPLY=( $( compgen -W "$(_get_examples)" -- "$cur" ) )
                                ;;
                        help)
-                               COMPREPLY=( $( compgen -W "$commands" -- "$cur" ) )
+                               COMPREPLY=( $( compgen -W "$__cargo_commands" -- "$cur" ) )
                                ;;
                        *)
                                local opt_var=opt__${cmd//-/_}
@@ -78,6 +77,8 @@ _cargo()
 } &&
 complete -F _cargo cargo
 
+__cargo_commands=$(cargo --list | tail -n +2)
+
 _locate_manifest(){
        local manifest=`cargo locate-project 2>/dev/null`
        # regexp-replace manifest '\{"root":"|"\}' ''